package cn.cimoc.service;

import cn.cimoc.mapper.HeroMapper;
import cn.cimoc.pojo.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;


@Service
public class HeroService {

    @Autowired
    HeroMapper mapper;

    public String insert(ModelMap map, String name, String type, long price) {
        if (mapper.findByName(name) > 0) {
            map.put("error", "英雄已存在");
            return "/add";
        }
        Hero hero = new Hero(name, type, price);
        mapper.insert(hero);
        Hero[] heroes = mapper.selectAll();
        map.put("list", heroes);
        return "/list";
    }

    public String delete(ModelMap map, long id) {
        mapper.deleteById(id);
        Hero[] heroes = mapper.selectAll();
        map.put("list", heroes);
        return "/list";
    }

    public String modify(ModelMap map, long id, String name, String type, long price) {
        Hero hero = new Hero(id, name, type, price);
        mapper.update(hero);
        Hero[] heroes = mapper.selectAll();
        map.put("list", heroes);
        return "/list";
    }

}
